<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.hasData()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/data/">数据操作</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.hasData/" target="_blank">jQuery.hasData()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-hasData1"><h2 class="section-title">
<span class="name">jQuery.hasData( element )</span><span class="returns">返回: <a href="/Types/#Boolean">Boolean</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>确定任何一个元素是否有与之相关的jQuery数据。</p>
<ul class="signatures"><li class="signature" id="jQuery-hasData-element">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.5/">1.5</a></span>jQuery.hasData( element )</h4>
<ul><li>
<div><strong>element</strong></div>
<div>类型: <a href="/Types/#Element">Element</a>
</div>
<div>一个用于进行检查数据的DOM元素。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>jQuery.hasData()</code>方法提供了一种方法来确定一个元素是否有任何数据，这些数据是使用<a href="/jQuery.data"><code>jQuery.data()</code></a>设置的。如果一个元素没有数据（没有在所有数据对象或数据对象是空的），该方法返回<code>false</code> ;否则返回<code>true</code> 。</p>
<p><code>jQuery.hasData(element)</code> 的主要优点是它并不创建 data 对象。如果元素上没有 data 对象，那么该方法也不会与元素上的 data 对象关联。相反，<code>jQuery.data(element)</code>总是向调用者返回一个 data 对象，即使该元素上不含有 data 对象，它也会创建一个。</p>
<p>
			      请注意，jQuery的事件系统是使用jQuery数据 存储事件处理程序的。
			      因此，使用<code>.on()</code>, <code>.bind()</code>, <code>.live()</code>, <code>.delegate()</code>，或一个速记事件方法 绑定事件到一个元素上的时候，也会在那个元素上关联一个 data 对象。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">元素上设置数据，看到hasData的结果。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">p</span>&gt;</span>Results: <span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> $p = jQuery(<span class="string">"p"</span>), p = $p[<span class="number">0</span>];</code></div></div><div class="container"><div class="line"><code>$p.append(jQuery.hasData(p)+<span class="string">" "</span>); <span class="comment">/* false */</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$.data(p, <span class="string">"testing"</span>, <span class="number">123</span>);</code></div></div><div class="container"><div class="line"><code>$p.append(jQuery.hasData(p)+<span class="string">" "</span>); <span class="comment">/* true*/</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$.removeData(p, <span class="string">"testing"</span>);</code></div></div><div class="container"><div class="line"><code>$p.append(jQuery.hasData(p)+<span class="string">" "</span>); <span class="comment">/* false */</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$p.on(<span class="string">'click'</span>, <span class="keyword">function</span>() {});</code></div></div><div class="container"><div class="line"><code>$p.append(jQuery.hasData(p)+<span class="string">" "</span>); <span class="comment">/* true */</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$p.off(<span class="string">'click'</span>);</code></div></div><div class="container"><div class="line"><code>$p.append(jQuery.hasData(p)+<span class="string">" "</span>); <span class="comment">/* false */</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>